package org.lemon.db.store;

import java.util.HashMap;
import java.util.Map;

/**
 * @since 2024-12-15 10:53
 */
public class BufferList {

    private BufferManager bufferManager;
    private Map<BlockId,Buffer> buffers = new HashMap<>();

    public BufferList(BufferManager bufferManager) {
        this.bufferManager = bufferManager;
    }

    public Buffer getBuffer(BlockId blockId){
        return buffers.get(blockId);
    }

    public void pin(BlockId blockId) {
        Buffer buffer = bufferManager.pin(blockId);
        buffers.put(blockId,buffer);
    }

    public void unpin(BlockId blockId) {
        Buffer buffer = buffers.get(blockId);
        if(buffer == null){
            return;
        }
        bufferManager.unpin(buffer);
        buffers.remove(blockId);
    }

    public void unpinAll(){
        buffers.values().forEach(buf-> bufferManager.unpin(buf));
        buffers.clear();
    }


}
